package com.steven.door.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.x6.serial.SerialUtil;
import com.steven.door.base.Constant;
import com.steven.door.base.SerialCode;
import com.steven.door.base.WEBCode;
import com.steven.door.utils.ByteUtils;
import com.steven.door.utils.SystemUtils;
import com.steven.door.utils.ToastUtils;

import java.util.Objects;

public class WEBReceive extends BroadcastReceiver implements WEBCode , SerialCode {
    @Override
    public void onReceive(Context context, Intent intent) {
        String type=intent.getStringExtra("type");
        assert type != null;
        switch (type){
            case Constant.server://下发的指令
                String data= intent.getStringExtra("data");
                boolean isJson=JSON.isValid(data);
                if (isJson){
                    JSONObject jsonObject = (JSONObject) JSON.parse(intent.getStringExtra("data"));
                    assert jsonObject != null;
                    String netType = jsonObject.getString("type");
                    switch (netType) {
                        case WEB_OPEN://开锁
                            int cases=Integer.parseInt(jsonObject.getString("cases"));
                            int doors=Integer.parseInt(jsonObject.getString("doors"));
                            byte[] open=ByteUtils.concatAll(doorHead, ByteUtils.intToHexByte(cases,doors),open_door,doorEnd);
                            SerialUtil.getInstance().openDoor(open);
                            byte[] watch = ByteUtils.concatAll(doorHead, ByteUtils.intToHexByte(cases,doors), query, doorEnd);
                            SerialUtil.getInstance().watchSerial(watch);
                            break;
                        case WEB_REGISTER://注册
                            ToastUtils.showToast(context, "注册");
                            break;
                        case WEB_RESTART://重启
                            SystemUtils.resetDevice();
                            break;
                        case WEB_UP://更新
                            ToastUtils.showToast(context, "更新");
                            break;
                        case WEB_HEART://心跳包
                            Log.e("WebSocketService", "onReceive: 心跳包" );
                            break;
                        case WEB_BIND://绑定成功
                            ToastUtils.showToast(context,"绑定成功");
                            break;
                        case WEB_LOGIN://登陆成功
//                            ToastUtils.showToast(context,"登陆成功");
                            break;
                    }
                }else{
                    Log.e("WEBReceive", "onReceive: beatHeart" );
                }

                break;
            case Constant.net://没网络
                ToastUtils.showToast(context,"请先配置网络~");
                break;
        }



    }
}
